﻿using System.Windows;
using System.Windows.Controls;
using compshop.Modules.ClientOrderList.Presenter;
using compshop.Infrastructure.CompShopDataService;


namespace compshop.Modules.ClientOrderList.Views
{
    /// <summary>
    /// Логика взаимодействия для ClientOrderListView.xaml
    /// </summary>
    public partial class ClientOrderListView : UserControl, IViewClientOrderList
    {
        public ClientOrderListPresenter Presenter { get; set; }

        public ClientOrderListView()
        {
            InitializeComponent();
        }

        public ClientOrderListView(ClientOrderListPresenter prsnt)
            :this()
        {
            Presenter = prsnt;
            Presenter.View = this;
            DataContext = prsnt;
        }

        private void cbStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            dgOrders.ItemsSource = Presenter.GetClientOrders(cbStatus.SelectedIndex, cbDepartment.SelectedItem as Department);
            dgOrders.Items.Refresh();
        }

        private void btnAnnulation_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ClienOrderAnnulate();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (dgOrders.SelectedItem != null)
            {
                ClientOrder order = dgOrders.SelectedItem as ClientOrder;
                if (order == null)
                    return;
                Presenter.CreateOrderEditWindow();
            }
        }

        private void btnAddNewOrder_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ShowNewClientOrderDialog();
        }

        private void dgOrders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgOrders.SelectedItem != null)
            {
                ClientOrder order = dgOrders.SelectedItem as ClientOrder;
                if (order == null)
                    return; 
                Presenter.onSelectedClientOrder((int)order.ClientOrderID);
            }
        }

        public void ShowData()
        {
        }

        public void UpdateData()
        {
            dgOrders.ItemsSource = null;
            dgOrders.ItemsSource = Presenter.ClientOrders;
            dgOrders.UpdateLayout();
        }

    }
}
